package com.inspur.gs.sbssdl.gzgl.ledger.ledger.back.commoncmps;

import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.core.LcpUtil;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.condition.EntityFilter;
import com.inspur.edp.cef.entity.condition.FilterCondition;
import com.inspur.edp.cef.entity.entity.AssoInfoBase;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.dxc.common.domain.CommonResult;
import com.inspur.edp.dxc.spi.components.BaseBeforeDBUpdateComponent;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.sysmanager.api.data.org.GSPSysOrg;
import io.iec.edp.caf.sysmanager.api.data.user.User;
import io.iec.edp.caf.sysmanager.api.manager.org.SysOrgManager;
import io.iec.edp.caf.sysmanager.api.manager.user.UserManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

public class GzImport extends BaseBeforeDBUpdateComponent {

    // 状态
    private final String ztCfg = "com.inspur.gs.sbssdl.gzgl.basedata.basedata.back.Gzzt";

    private List<User> users = new ArrayList<>();
    private List<GSPSysOrg> allOrgs = new ArrayList<>();
    private HashMap<String,IEntityData> ztMap = new HashMap<>();

    public void importDataSetBefore(){
        List<ModifyChangeDetail> details = super.getDetails();

        UserManager userManager = SpringBeanUtils.getBean(UserManager.class);
        users = userManager.getUsers();

        SysOrgManager sysOrgManager = SpringBeanUtils.getBean(SysOrgManager.class);
        allOrgs = sysOrgManager.getAllOrgs();

        details = details.stream()
                .peek(this::fillRelationField)
                .collect(Collectors.toList());

        CommonResult result = super.getResult();
        result.setChangeDetails(details);
    }


    // 填充关联字段
    private void fillRelationField(ModifyChangeDetail detail) {
        // 状态
        AssoInfoBase info = (AssoInfoBase) detail.getPropertyChanges().get("zt");
        if (info != null) {
            String zt = (String) info.getValue("zt");

            if (ztMap.containsKey(zt)) {
                info.setValue("zt", ztMap.get(zt).getID());
                info.setValue("zt_name", ztMap.get(zt).getValue("name"));
            }else {
                IStandardLcp lcp = LcpUtil.getLcp(ztCfg);
                EntityFilter filter = new EntityFilter();
                ArrayList<FilterCondition> filters = new ArrayList<>(FilterCondition.builder()
                        .andLike("name", zt)
                        .build());
                filter.setFilterConditions(filters);
                List<IEntityData> query = lcp.query(filter);
                if (!query.isEmpty()) {
                    IEntityData iData = query.get(0);
                    info.setValue("zt", iData.getID());
                    info.setValue("zt_name", iData.getValue("name"));
                    ztMap.put(zt, iData);
                }
            }
        }

        // 部门
        info = (AssoInfoBase) detail.getPropertyChanges().get("bmid");
        if (info != null) {
            String bmbh_name = (String) info.getValue("bmid");

            List<GSPSysOrg> data = allOrgs.stream().filter(i -> i.getName().equals(bmbh_name)).collect(Collectors.toList());

            if (!data.isEmpty()) {
                info.setValue("bmid", data.get(0).getId());
                info.setValue("bmid_name", data.get(0).getName());
            }
        }


        // 责任人
        info = (AssoInfoBase) detail.getPropertyChanges().get("zrr");
        if (info != null) {
            String zrr_name = (String) info.getValue("zrr");
            String zrr_code = (String) info.getValue("zrr_code");

            List<User> data = users.stream().filter(i -> i.getName().equals(zrr_name) && i.getCode().equals(zrr_code)).collect(Collectors.toList());

            if (!data.isEmpty()) {
                info.setValue("zrr", data.get(0).getId());
                info.setValue("zrr_name", data.get(0).getName());
            }
        }
    }
}